package tr.richfacesext.components.captcha;

import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

/**
 * 
 * @author mert
 *
 */
public class Captcha extends UIInput{

	public static final String COMPONENT_TYPE = "tr.richfacesext.components.Captcha";
	public static final String COMPONENT_FAMILY = "tr.richfacesext.components";
	private static final String DEFAULT_RENDERER = "tr.richfacesext.components.captcha.CaptchaRenderer";

	private Integer _maxLength;
	private Integer _letterWidth;
	private Integer _imgHeight;
	private Double _imgSkew;
	private Integer _drawLines;
	private Integer _drawBoxes;
	private Boolean _invertColors;

	public Captcha() {
		setRendererType(DEFAULT_RENDERER);
	}

	public String getFamily() {
		return COMPONENT_FAMILY;
	}

	public Integer getMaxLength() {
		if(_maxLength != null )
			return _maxLength;

		ValueBinding vb = getValueBinding("maxLength");
		return vb != null ? (java.lang.Integer) vb.getValue(getFacesContext()) : 6;
	}
	public void setMaxLength(Integer maxLengthValue) {
		_maxLength = maxLengthValue;
	}

	public Integer getLetterWidth() {
		if(_letterWidth != null )
			return _letterWidth;

		ValueBinding vb = getValueBinding("letterWidth");
		return vb != null ? (java.lang.Integer) vb.getValue(getFacesContext()) : 33;
	}
	public void setLetterWidth(Integer letterWidthValue) {
		_letterWidth = letterWidthValue;
	}

	public Integer getImgHeight() {
		if(_imgHeight != null )
			return _imgHeight;

		ValueBinding vb = getValueBinding("imgHeight");
		return vb != null ? (java.lang.Integer) vb.getValue(getFacesContext()) : 40;
	}
	public void setImgHeight(Integer imgHeightValue) {
		_imgHeight = imgHeightValue;
	}

	public Double getImgSkew() {
		if(_imgSkew != null )
			return _imgSkew;

		ValueBinding vb = getValueBinding("imgSkew");
		return vb != null ? (java.lang.Double) vb.getValue(getFacesContext()) : 0.5;
	}
	public void setImgSkew(Double imgSkewValue) {
		_imgSkew = imgSkewValue;
	}

	public Integer getDrawLines() {
		if(_drawLines != null )
			return _drawLines;

		ValueBinding vb = getValueBinding("drawLines");
		return vb != null ? (java.lang.Integer) vb.getValue(getFacesContext()) : 1;
	}
	public void setDrawLines(Integer drawLinesValue) {
		_drawLines = drawLinesValue;
	}

	public Integer getDrawBoxes() {
		if(_drawBoxes != null )
			return _drawBoxes;

		ValueBinding vb = getValueBinding("drawBoxes");
		return vb != null ? (java.lang.Integer) vb.getValue(getFacesContext()) : 100;
	}
	public void setDrawBoxes(Integer drawBoxesValue) {
		_drawBoxes = drawBoxesValue;
	}

	public Boolean getInvertColors() {
		if(_invertColors != null )
			return _invertColors;

		ValueBinding vb = getValueBinding("invertColors");
		return vb != null ? (java.lang.Boolean) vb.getValue(getFacesContext()) : Boolean.FALSE;
	}
	public void setInvertColors(Boolean invertColorsValue) {
		_invertColors = invertColorsValue;
	}

	public Object saveState(FacesContext context) {
		Object values[] = new Object[8];
		values[0] = super.saveState(context);
		values[1] = _maxLength;
		values[2] = _letterWidth;
		values[3] = _imgHeight;
		values[4] = _imgSkew;
		values[5] = _drawLines;
		values[6] = _drawBoxes;
		values[7] = _invertColors;
		return ((Object) values);
	}
	public void restoreState(FacesContext context, Object state) {
		Object values[] = (Object[]) state;
		super.restoreState(context, values[0]);
		_maxLength = (Integer) values[1];
		_letterWidth = (Integer) values[2];
		_imgHeight = (Integer) values[3];
		_imgSkew = (Double) values[4];
		_drawLines = (Integer) values[5];
		_drawBoxes = (Integer) values[6];
		_invertColors = (Boolean) values[7];
	}
}